package com.example.nutritionmanager.di

import android.content.Context
import com.example.nutritionmanager.api.AuthInterceptor
import com.example.nutritionmanager.api.CalorieService
import com.example.nutritionmanager.api.NutritionService
import com.example.nutritionmanager.api.ResultService
import com.example.nutritionmanager.api.UserService
import com.example.nutritionmanager.data.TokenDataStore
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {

    private const val FASTAPI_BASE_URL = "http://localhost:8181/"
    private const val SPRING_BASE_URL = "http://localhost:8080/" // 替换为你的后端地址

    @Provides
    @Singleton
    fun provideTokenDataStore(@dagger.hilt.android.qualifiers.ApplicationContext context: Context): TokenDataStore {
        return TokenDataStore(context)
    }

    @Provides
    @Singleton
    fun provideAuthInterceptor(tokenDataStore: TokenDataStore): AuthInterceptor {
        return AuthInterceptor {
            // 同步获取 Token
            tokenDataStore.getTokenSync()
        }
    }

    @Provides
    @Singleton
    fun provideOkHttpClient(authInterceptor: AuthInterceptor): OkHttpClient {
        return OkHttpClient.Builder()
            .addInterceptor(authInterceptor)
            .build()
    }

    @Provides
    @Singleton
    fun provideGson(): Gson = GsonBuilder().create()

    @Provides
    @Singleton
    @SpringApi
    fun provideSpringRetrofit(gson: Gson, okHttpClient: OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .baseUrl(SPRING_BASE_URL)
            .client(okHttpClient) // 使用自定义 OkHttpClient
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build()
    }

    // 提供 FastAPI 的 Retrofit 实例
    @Provides
    @Singleton
    @FastApi
    fun provideFastRetrofit(gson: Gson, okHttpClient: OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .baseUrl(FASTAPI_BASE_URL)
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build()
    }

    @Provides
    @Singleton
    fun provideUserService(@SpringApi retrofit: Retrofit): UserService {
        return retrofit.create(UserService::class.java)
    }

    @Provides
    @Singleton
    fun provideNutritionService(@SpringApi retrofit: Retrofit): NutritionService {
        return retrofit.create(NutritionService::class.java)
    }

    // 使用 FastAPI Retrofit 实例创建 OCR 服务
    @Provides
    @Singleton
    fun provideResultService(@FastApi retrofit: Retrofit): ResultService {
        return retrofit.create(ResultService::class.java)
    }

    @Provides
    @Singleton
    fun provideCalorieService(@SpringApi retrofit: Retrofit): CalorieService {
        return retrofit.create(CalorieService::class.java)
    }

}
